package com.px.project.module.it.listener;

import java.util.Date;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import com.px.common.config.security.LoginContextHolder;
import com.px.common.enums.ApproveStatusEnum;
import com.px.project.module.it.entity.WeekPlanDao;
import com.px.project.module.it.service.IWeekPlanService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 智能排产》》施工计划》》周计划工作流监听业务逻辑实现
 *
 * @author 品讯科技
 * @date 2024-08
 */
@Slf4j
@Service(value = "itWeekPlanDeleteTaskListener")
@RequiredArgsConstructor
public class WeekPlanDeleteTaskListener implements TaskListener {
    private static final Integer DELETED_ON = 0;

    private static final String EVENT_NAME_DELETE = "delete";

    private final IWeekPlanService weekPlanService;

    /**
     * 删除事件监听
     * 
     * @param delegateTask DelegateTask
     */
    @Override
    public void notify(DelegateTask delegateTask) {
        TaskEntity taskEntity = (TaskEntity)delegateTask;
        log.info("Month Plan Delete Task, taskName = {}  BusinessKey = {}", taskEntity.getName(), taskEntity.getBusinessKey());
        Long id = Long.valueOf(taskEntity.getBusinessKey());
        /* 拒绝通过 */
        if (ObjectUtil.equal(EVENT_NAME_DELETE, taskEntity.getEventName()) && taskEntity.isCanceled()) {
            UpdateWrapper<WeekPlanDao> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().set(WeekPlanDao::getUpdateTime, new Date()).set(WeekPlanDao::getUpdateBy, LoginContextHolder.me().getId())
                .set(WeekPlanDao::getApproveStatus, ApproveStatusEnum.REJECT.getCode()).eq(WeekPlanDao::getId, id)
                .eq(WeekPlanDao::getDeleted, DELETED_ON);
            this.weekPlanService.update(updateWrapper);
        }
    }
}